﻿using System.Text.RegularExpressions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using TimerTask.Common.Utils;

namespace TimerTask.Web.Controllers
{
    public class BaseController : Controller
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            string token = HttpContext.Request.Cookies["token"];

            if (token == null)
            {
                context.Result = RedirectToAction("Index", "Account");
            }
            else
            {
                token = Base64Util.DeBase64(token);
                Regex regex = new Regex(@"^.+?[|].{32}$");
                if (!regex.Match(token).Success)
                {
                    context.Result = RedirectToAction("Index", "Account");
                }
            }
        }
    }
}
